﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;


namespace _1.OrderService
{
    public partial class ModifyDetailForm : Form
    {
        ModifyForm _modifyForm;
        OrderService _orderService;
        long _orderID;
        AutoSizeFormClass asc = new AutoSizeFormClass();
        public ModifyDetailForm(ModifyForm modifyForm, OrderService orderService, long orderID)
        {
            InitializeComponent();
            _modifyForm = modifyForm;
            _orderService = orderService;
            _orderID = orderID;
            for(int i = 0; i < orderService.orders.Count; ++i)
            {
                if(orderService.orders[i].orderID==orderID)
                {
                    textBox_id.Text = orderService.orders[i].orderID.ToString();
                    textBox_product.Text = orderService.orders[i].orderDetail.productName;
                    textBox_custom.Text = orderService.orders[i].orderDetail.customer;
                    textBox_money.Text = orderService.orders[i].orderDetail.orderAmount.ToString();
                    break;
                }
            }
            this.SizeChanged += Form_SizeChanged;
        }

        private void ModifyDetailForm_Load(object sender, EventArgs e)
        {
            asc.controllInitializeSize(this);
        }

        private void Form_SizeChanged(object sender, EventArgs e)
        {
            asc.controlAutoSize(this);
        }
        private void button_quit_Click(object sender, EventArgs e)
        {
            _modifyForm.Show();
            this.Close();
        }

        private bool IsAllNumber(string text)
        {
            Regex objNotNumberPattern = new Regex("[^0-9.-]");
            Regex objTwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*");
            Regex objTwoMinusPattern = new Regex("[0-9]*[-][0-9]*[-][0-9]*");
            String strValidRealPattern = "^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
            String strValidIntegerPattern = "^([-]|[0-9])[0-9]*$";
            Regex objNumberPattern = new Regex("(" + strValidRealPattern + ")|(" + strValidIntegerPattern + ")");
            return !objNotNumberPattern.IsMatch(text) && !objTwoDotPattern.IsMatch(text) && !objTwoMinusPattern.IsMatch(text) && objNumberPattern.IsMatch(text);
        }

        private void button_modify_Click(object sender, EventArgs e)
        {
            string product = textBox_product.Text;
            string custom = textBox_custom.Text;
            string moneyStr = textBox_money.Text;
            if(!IsAllNumber(moneyStr))
            {
                DialogResult dr1 = MessageBox.Show("金额输入不合法请重新输入", "警告", MessageBoxButtons.OK);
                textBox_money.Text = "";
                return;
            }
            double money = double.Parse(moneyStr);
            _orderService.modifyOrder(_orderID, product, custom, money);
            DialogResult dr = MessageBox.Show("修改成功", "警告", MessageBoxButtons.OK);
            if(dr==DialogResult.OK)
            {
                _modifyForm.Show();
                this.Close();
            }
        }
    }
}
